package com.yfp.goods.service.impl;

import com.yfp.goods.domain.GoodsDetailImg;
import com.yfp.goods.mapper.GoodsDetailImgMapper;
import com.yfp.goods.service.GoodsDetailImgService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 商品详细图片视频表(GoodsDetailImg)表服务实现类
 *
 *
 * @since 2023-12-25 17:29:39
 */
@Service("goodsDetailImgService")
public class GoodsDetailImgServiceImpl implements GoodsDetailImgService {
    @Resource
    private GoodsDetailImgMapper goodsDetailImgMapper;

    @Resource
    private ComboDetailImgServiceImpl comboDetailImgService;

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public List<GoodsDetailImg> queryById(String goodsId) {
        return this.goodsDetailImgMapper.queryById(goodsId);
    }

    /**
     * 分页查询
     *
     * @param goodsDetailImg 筛选条件
     * @return 查询结果
     */
    @Override
    public List<GoodsDetailImg> queryAll(GoodsDetailImg goodsDetailImg) {
        return this.goodsDetailImgMapper.queryAll(goodsDetailImg);
    }

    /**
     * 新增数据
     *
     * @param goodsDetailImg 实例对象
     * @return 实例对象
     */
    @Override
    public GoodsDetailImg insert(GoodsDetailImg goodsDetailImg) {
        this.goodsDetailImgMapper.insert(goodsDetailImg);
        return goodsDetailImg;
    }

    /**
     * 按商品id查询
     *
     * @param goodsId 货物id
     * @return {@link List}<{@link GoodsDetailImg}>
     */
    @Override
    public List<GoodsDetailImg> queryByGoodsId(String goodsId) {
        GoodsDetailImg goodsDetailImg = new GoodsDetailImg();
        goodsDetailImg.setGoodsId(goodsId);
        List<GoodsDetailImg> goodsDetailImgs = this.goodsDetailImgMapper.queryAll(goodsDetailImg);
        return goodsDetailImgs.stream().map(goodsDetail -> {
            String resourceUrl = comboDetailImgService.getResourceUrl(goodsDetail.getResourceType(), goodsDetail.getResourceId());
            if (Objects.nonNull(resourceUrl)) goodsDetail.setResourceUrl(resourceUrl);
            return goodsDetail;
        }).toList();
    }

    /**
     * 插入或更新批
     *
     * @param goodsDetailImgs 货物详细信息
     * @return int
     */
    @Override
    public int insertOrUpdateBatch(List<GoodsDetailImg> goodsDetailImgs) {
        return this.goodsDetailImgMapper.insertOrUpdateBatch(goodsDetailImgs);
    }

    /**
     * 更新
     *
     * @param goodsDetailImg 商品明细img
     * @return int
     */
    @Override
    public int update(GoodsDetailImg goodsDetailImg) {
       return this.goodsDetailImgMapper.update(goodsDetailImg);
    }

    /**
     * 删除
     *
     * @param id
     * @return boolean
     */
    @Override
    public boolean delete(String id) {
        return this.goodsDetailImgMapper.delete(Long.valueOf(id));
    }

}
